﻿package com.taihua_tech.basic.modul2.service;

import javax.annotation.Resource;

import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.taihua_tech.basic.modul2.dao.QiuNingDao;
import com.taihua_tech.basic.util.DataParam;
import com.taihua_tech.basic.util.DataRow;
 
//注入bookDao
//调用bookDao.findByUserId,将结果封装成NoteResult

@Service("qiuNingService")
@Transactional
public class QiuNingServiceImpl implements QiuNingService{
	@Resource
	private QiuNingDao qiuNingDao;
	
	public String shiwuTest(DataParam param) throws Exception {
		JSONObject jsonObj=new JSONObject(); 
		/************事务测试，把 第一条记录的 值 减掉，加在 第二条记录 的身上*************/
		int money=param.getInt("MONEY");  
		//1.第一条记录 减 操作
		param.put("ID", param.getInt("ID1"));
		DataRow rowA=qiuNingDao.getQiuNingRow(param);
		int ddA=rowA.getInt("DD");
		if(ddA<money){
			jsonObj.put("result","fail" );
			jsonObj.put("message","余额额不足！" );
			return jsonObj.toJSONString();
		}
		DataParam paramA=new DataParam ();
		paramA.append(rowA);
		paramA.put("DD", ddA-money);
		qiuNingDao.updateQiuNing(paramA);
		//在这里模拟异常发生
		try {
			int i=5/0;
		} catch (Exception e) {
			throw e;
		}
		
		//1.第二条记录 加 操作
		param.put("ID", param.getInt("ID2"));
		DataRow rowB=qiuNingDao.getQiuNingRow(param);
		int ddB=rowB.getInt("DD");
		DataParam paramB=new DataParam ();
		paramB.append(rowB);
		paramB.put("DD", ddB+money);
		qiuNingDao.updateQiuNing(paramB);
		
		jsonObj.put("result","sucess" );
		jsonObj.put("message","转账成功！" );
		return jsonObj.toJSONString();
	}

}
